/************************************************************************************
* PlaylistManager.cpp                                                                        *
*                                                                                   *
* Copyright (C) 2011 Varuna L Amachi                                              *
*                                                                                   *
* This program is free software; you can redistribute it and/or modify              *
* it under the terms of the GNU General Public License as published by              *
* the Free Software Foundation; either version 2 of the License, or any             *
* later version.                                                                    *
*                                                                                   *
* This program is distributed in the hope that it will be useful, but               *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for       *
* more details. You should have received a copy of the GNU General Public           *
* License along with this program; if not, write to the Free Software Foundation,   *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                       *
*                                                                                   *
*************************************************************************************/ 

#include "PlaylistManager.h"

PlaylistManager::PlaylistManager(QObject *parent) :
    QAbstractItemModel(parent)
{
    m_playlistMap = new QHash<QString, SavedPlaylist *>();
}



QList<SavedPlaylist *> PlaylistManager::getPlaylists(){
    return m_playlistMap->values();
}


SavedPlaylist *PlaylistManager::getPlaylist(QString playlistName){
    return m_playlistMap->value(playlistName);
}


void PlaylistManager::addPlaylist(SavedPlaylist *playlist){
    m_playlistMap->insert(playlist->playlistName(), playlist);
    beginInsertRows(QModelIndex(), m_playlists.size(), m_playlists.size());
    m_playlists.append(playlist);
    endInsertRows();
}


void PlaylistManager::removePlaylist(QString playlistName) {
    int index = m_playlists.indexOf(m_playlistMap->value(playlistName));
    beginRemoveRows(QModelIndex(), index, index);
    m_playlistMap->remove(playlistName);
    endInsertRows();
}


int PlaylistManager::rowCount(const QModelIndex &parent) const{
    return m_playlists.size();
}


int PlaylistManager::columnCount(const QModelIndex &parent) const{
    return 4;
}


QVariant PlaylistManager::data(const QModelIndex &index, int role) const{
    if(index.isValid()) {
        SavedPlaylist *playlist = m_playlists.at(index.row());
        switch(index.column()) {
        case 0: return playlist->playlistName();
        case 1: return playlist->comment();
        case 2: return playlist->entries()->size();
        case 3: return playlist->rating();
        }
    }
    return QVariant();
}


QVariant PlaylistManager::headerData(int section, Qt::Orientation orientation, int role) const{

        switch(section) {
        case 0: return tr("Name");
        case 1: return tr("Description");
        case 2: return tr("Count");
        case 3: return tr("Rating");

    }
    return QVariant();
}


QModelIndex PlaylistManager::index(int row, int column, const QModelIndex &parent) const{
    return createIndex(row, column);
}


QModelIndex PlaylistManager::parent(const QModelIndex &child) const{
    return QModelIndex();
}

bool PlaylistManager::hasChildren(const QModelIndex &parent) const{
    if(! parent.isValid()) {
        return true;
    }
    return false;
}


void PlaylistManager::resetList() {
    beginResetModel();
    m_playlists = m_playlistMap->values();
    endResetModel();
}
